------------------------------------------------------------------------------------------------------------------------------------------------
--[[
    【信】信：8天后自动结束，或理智与饱度都回复到78%以上后结束。
    【信】持续期间内不会进入【不朽尸王】状态，失去0.22攻击系数，移动系数（也就是攻击*0.78，移速0.78），
    拥有回血光环，回复自身及身边玩家18HP/min， 0.3HP/S
    若胡桃处于此状态，则无法进入【奔跑】状态。
]]--
------------------------------------------------------------------------------------------------------------------------------------------------
---
    local DEBUFF_REMAIN_TIME = 8*480 -- 效果持续时间
------------------------------------------------------------------------------------------------------------------------------------------------
--- 素材
    local assets =
    {

    }
------------------------------------------------------------------------------------------------------------------------------------------------
--- debuff 激活
    local function OnAttached(inst,player) -- 玩家得到 debuff 的瞬间。 穿越洞穴、重新进存档 也会执行。
        inst.entity:SetParent(player.entity)
        inst.Transform:SetPosition(0,0,0)
        -----------------------------------------------------
        --- 计时器初始化
            if inst.components.hutao_data:Get("timer") == nil then
                inst.components.hutao_data:Set("timer",DEBUFF_REMAIN_TIME)
                inst.__timer:set(DEBUFF_REMAIN_TIME)
                player.components.hutao_com_inspect_data:Set("hutao_debuff_undying_lich_after_wakeup",DEBUFF_REMAIN_TIME)
            end
            inst:DoPeriodicTask(1,function()
                local timer = inst.components.hutao_data:Add("timer",-1)
                player.components.hutao_com_inspect_data:Set("hutao_debuff_undying_lich_after_wakeup",timer)
                if timer <= 0 then
                    inst:Remove()
                end
                inst.__timer:set(timer)
            end)
            inst:ListenForEvent("onremove",function()                
                player.components.hutao_com_inspect_data:Set("hutao_debuff_undying_lich_after_wakeup",0)
            end)
        -----------------------------------------------------
        --- 参数控制
            inst:DoPeriodicTask(1,function()
                if player.replica.hutao_com_physical_power then
                    player.replica.hutao_com_physical_power:AddBlocker(inst)
                end
                if player.components.combat then
                    player.components.combat.externaldamagemultipliers:SetModifier(inst,1-0.22)
                end
                if player.components.locomotor then
                    player.components.locomotor:SetExternalSpeedMultiplier(inst,inst.prefab, 1-0.22)
                end
                --- 回血光环
                local x,y,z = player.Transform:GetWorldPosition()
                local ents = TheSim:FindEntities(x,0, z,15, {"player"}, {"playerghost"})
                for k, temp_player in pairs(ents) do
                    if temp_player.components.health and not temp_player.components.health:IsDead() then
                        temp_player.components.health:DoDelta(0.3,true)
                    end
                end

            end)
        -----------------------------------------------------
        --- 移除
            inst:ListenForEvent("onremove",function()                
 
            end)
        -----------------------------------------------------
        --- 目标。周期性刷一下，避免无法启动。
            local function set_target_fn()
                inst.__target:set( inst.__target:value() == player and inst or player)
            end
            set_target_fn()
            inst:DoPeriodicTask(1,set_target_fn)            
        -----------------------------------------------------
        --- 周期性检查是否需要解除
            inst:DoPeriodicTask(0.5,function()
                if player.components.sanity:GetPercent() >= 0.78 and player.components.hunger:GetPercent() >= 0.78 then
                    inst:Remove()
                end
            end)
        -----------------------------------------------------
        -- 死亡清除
            inst:ListenForEvent("death",function()
                inst:Remove()
            end,player)
        -----------------------------------------------------
    end

    local function ExtendDebuff(inst) --- 重复得到debuff的瞬间
        
    end
------------------------------------------------------------------------------------------------------------------------------------------------
--- hud mask
    local function create_buff_mask_in_client_side(inst)
        -----------------------------------------------------
        --- 预检查 和 模块引入
            local Widget = require "widgets/widget"
            local Image = require "widgets/image"
            local Text = require "widgets/text"
        -----------------------------------------------------
        --- 根节点
            local front_root = ThePlayer.HUD
            local root = front_root:AddChild(Widget())
            root:SetHAnchor(0) -- 设置原点x坐标位置，0、1、2分别对应屏幕中、左、右
            root:SetVAnchor(0) -- 设置原点y坐标位置，0、1、2分别对应屏幕中、上、下
            root:SetPosition(0,0)
            -- root:MoveToFront()
            root:MoveToBack()
            root:SetScaleMode(SCALEMODE_FIXEDSCREEN_NONDYNAMIC)   --- 缩放模式
            root:SetClickable(false)
        -----------------------------------------------------
        ---
        -----------------------------------------------------
        --- 图层
            -- local img = root:AddChild(Image())
            -- img:SetTexture("images/widgets/hutao_debuff_undying_lich_mask.xml","hutao_debuff_undying_lich_mask.tex")
            -- -- local scale = 2.2
            -- -- img:SetScale(scale,scale,scale)
            -- img:SetFadeAlpha(0.7)
        -----------------------------------------------------
        --- 文本
            local time_str = root:AddChild(Text(CODEFONT,40,"",{ 255/255 , 255/255 ,255/255 , 1}))
            local time_pt = Vector3(-200,320,0)
            time_str:SetPosition(time_pt.x,time_pt.y)
        -----------------------------------------------------
        --- 
            root.inst:ListenForEvent("timer_update",function()
                local time = inst.__timer:value()
                --- time 单位：秒。
                local display_time = ""
                if time >= 60 then
                    display_time = string.format("%02d:%02d",math.floor(time/60),math.floor(time%60))
                else
                    display_time = tostring(math.floor(time))
                end
                time_str:SetString(tostring(inst:GetDisplayName()).." : ".. display_time)
            end,inst)
        -----------------------------------------------------
        --- 移除
            root.inst:ListenForEvent("onremove",function()
                root:Kill()
            end,inst)
        -----------------------------------------------------
        ---
            return root
        -----------------------------------------------------
    end
    local function create_mask(inst)
        if inst.__hud_mask == nil then
            inst.__hud_mask = create_buff_mask_in_client_side(inst)
        end
    end
    local function hud_mask_install(inst)
        local link_entity = inst.__target:value()
        if link_entity == ThePlayer and ThePlayer and ThePlayer.HUD then
            create_mask(inst)
            if ThePlayer.replica.hutao_com_physical_power then
                ThePlayer.replica.hutao_com_physical_power:AddBlocker(inst)
            end
        end
    end
------------------------------------------------------------------------------------------------------------------------------------------------
--- 实体
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddNetwork()
        inst.entity:AddTransform()

        inst:AddTag("CLASSIFIED")

        inst.entity:SetPristine()
        inst.__target = net_entity(inst.GUID,"hutao_debuff_undying_lich_after_wakeup.target","linked_player")
        inst.__timer = net_float(inst.GUID,"hutao_debuff_undying_lich_after_wakeup.timer","timer_update")

        if not TheNet:IsDedicated() then
            inst:ListenForEvent("linked_player",hud_mask_install)
        end
        if not TheWorld.ismastersim then
            return inst
        end

        inst:AddComponent("hutao_data")
        inst:AddComponent("debuff")
        inst.components.debuff:SetAttachedFn(OnAttached)
        inst.components.debuff.keepondespawn = true -- 是否保持debuff 到下次登陆
        inst.components.debuff:SetExtendedFn(ExtendDebuff)

        return inst
    end
------------------------------------------------------------------------------------------------------------------------------------------------

return Prefab("hutao_debuff_undying_lich_after_wakeup", fn,assets)
